//
//  FKOrderEvaluateCell.m
//  HappyTest
//
//  Created by Macbook Pro on 2019/2/25.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKOrderEvaluateCell.h"
#import "FKPlaceholderTextView.h"
#import "FKSelectPhotoView.h"
#import "FKCommodityOfEvaluate.h"

@interface FKOrderEvaluateCell ()<FKSelectPhotoViewDelegate>

@property (nonatomic, weak) UIImageView *picView;
@property (nonatomic, weak) UILabel *nameLabel;
@property (nonatomic, weak) UILabel *specLabel;
@property (nonatomic, weak) UIView *starContainer;
@property (nonatomic, weak) FKPlaceholderTextView *textView;
@property (nonatomic, weak) FKSelectPhotoView *photoView;

@property (nonatomic, assign) NSInteger star;

@end

@implementation FKOrderEvaluateCell

- (void)setupSubviews
{
    [super setupSubviews];
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    UIImageView *picView = [[UIImageView alloc] init];
    [self.contentView addSubview:picView];
    self.picView = picView;
    
    UILabel *nameLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333];
    [self.contentView addSubview:nameLabel];
    self.nameLabel = nameLabel;
    nameLabel.numberOfLines = 2;
    
    UILabel *specLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor999999];
    [self.contentView addSubview:specLabel];
    self.specLabel = specLabel;
    
    UIView *separator = [[UIView alloc] init];
    [self.contentView addSubview:separator];
    separator.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UILabel *titleLabel = [UILabel fk_labelWithFont:fk_adjustFont(14) textColor:fkColor333333 text:@"为商品评分"];
    [self.contentView addSubview:titleLabel];
    
    UIView *starContainer = [[UIView alloc] init];
    [self.contentView addSubview:starContainer];
    self.starContainer = starContainer;
    
    CGFloat starWH = fk_adjustW(26);
    CGFloat margin = fk_adjustW(24);
    CGFloat beginX = (fkScreenW - starWH * 5 - margin * 4) * 0.5;
    self.star = 5;
    for (NSInteger index = 0; index < 5; index++) {
        UIButton *starBtn = [UIButton fk_btnWithTarget:self action:@selector(starBtnClicked:) img:[UIImage imageNamed:@"evaluation_icon"]];
        [starBtn setImage:[UIImage imageNamed:@"evaluation_icon_fill"] forState:UIControlStateSelected];
        [starContainer addSubview:starBtn];
//        starBtn.selected = true; // 让数据来控制是否选中
        
        CGFloat starX = beginX + index * (starWH + margin);
        starBtn.frame = CGRectMake(starX, 0, starWH, starWH);
    }
    
    UIView *separator1 = [[UIView alloc] init];
    [self.contentView addSubview:separator1];
    separator1.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    FKPlaceholderTextView *textView = [[FKPlaceholderTextView alloc] init];
    [self.contentView addSubview:textView];
    self.textView = textView;
    textView.placeholder = @"宝贝满足你的期待吗？请说说吧";
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEdit) name:UITextViewTextDidEndEditingNotification object:nil];
    
    FKSelectPhotoView *photoView = [[FKSelectPhotoView alloc] init];
    [self.contentView addSubview:photoView];
    self.photoView = photoView;
    photoView.column = 4;
    photoView.delegate = self;
    
    [picView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(fk_adjustW(70));
        make.left.offset(12);
        make.top.offset(10);
    }];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(picView).offset(4);
        make.left.equalTo(picView.mas_right).offset(8);
        make.right.offset(-12);
    }];
    [specLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(picView).offset(-4);
        make.left.right.equalTo(nameLabel);
    }];
    [separator mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(picView);
        make.right.equalTo(nameLabel);
        make.top.equalTo(picView.mas_bottom).offset(10);
        make.height.mas_equalTo(0.8);
    }];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(separator.mas_bottom).offset(20);
        make.centerX.offset(0);
    }];
    [starContainer mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.mas_equalTo(fk_adjustW(28));
        make.top.equalTo(titleLabel.mas_bottom).offset(fk_adjustW(36));
    }];
    [separator1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.height.equalTo(separator);
        make.top.equalTo(starContainer.mas_bottom).offset(fk_adjustW(32));
    }];
    [textView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(separator);
        make.top.equalTo(separator1.mas_bottom).offset(15);
        make.height.mas_equalTo(fk_adjustW(80));
    }];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)endEdit
{
    self.commodity.content = self.textView.text;
}

- (void)starBtnClicked:(UIButton *)btn
{
    NSInteger btnIndex = [self.starContainer.subviews indexOfObject:btn];
    for (NSInteger index = 0; index < self.starContainer.subviews.count; index++) {
        UIButton *currentBtn = self.starContainer.subviews[index];
        if (index < btnIndex) {
            currentBtn.selected = true;
        } else if (index == btnIndex) {
            currentBtn.selected = true;
            self.star = btnIndex + 1;
            self.commodity.star = fkFORMAT(@"%zd", self.star);
        } else {
            currentBtn.selected = false;
        }
    }
}

#pragma mark - FKSelectPhotoViewDelegate
- (void)selectPhotoViewAdjustFrame:(FKSelectPhotoView *)view
{
    if ([self.delegate respondsToSelector:@selector(orderEvaluateCellPhotoViewChange:)]) {
        NSMutableDictionary *photoDict = [NSMutableDictionary dictionary];
        for (NSInteger index = 0; index < view.images.count; index++) {
            NSString *key = fkFORMAT(@"img%zd", index);
            photoDict[key] = view.images[index];
        }
        self.commodity.photoDict = photoDict;
        
        self.commodity.photoViewFrame = view.frame;
        self.commodity.cellH = CGRectGetMaxY(view.frame) + 15;
        [self.delegate orderEvaluateCellPhotoViewChange:self];
    }
}

- (void)setCommodity:(FKCommodityOfEvaluate *)commodity
{
    _commodity = commodity;
    
    [self.picView sd_setImageWithURL:[NSURL URLWithString:commodity.img] placeholderImage:kPlaceholder_1x1];
    self.nameLabel.text = commodity.title;
    self.specLabel.text = fkFORMAT(@"规格: %@", commodity.item_sku_name);
    self.photoView.frame = commodity.photoViewFrame;
    
    NSInteger star = [commodity.star integerValue];
    for (NSInteger index = 0; index < self.starContainer.subviews.count; index++) {
        UIButton *starBtn = self.starContainer.subviews[index];
        if (index < star) {
            starBtn.selected = true;
        } else {
            starBtn.selected = false;
        }
    }
}

@end
